package net.cassandraview.db;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.List;

import org.apache.cassandra.thrift.Column;
import org.apache.cassandra.thrift.ColumnOrSuperColumn;
import org.apache.cassandra.thrift.Mutation;
import org.apache.cassandra.thrift.SuperColumn;

public class DbUtils {

	protected static Charset defaultCharset = Charset.forName("UTF-8");
	
	/**
	 * Create {@link Column} with given key/value and System.nanoTime() as timestamp.
	 */
	public static Column createColumn(String name, String value) {
		return createColumn(name, value, System.nanoTime());
	}
	
	/**
	 * Create {@link Column} with given key/value and  timestamp.
	 */
	public static Column createColumn(String name, String value, Long timestamp) {
		return createColumn(bytesFromString(name), bytesFromString(value), System.nanoTime());
	}
	
	/**
	 * Create {@link Column} with given key/value and System.nanoTime() as timestamp.
	 */
	public static Column createColumn(ByteBuffer name, ByteBuffer value) {
		return createColumn(name, value, System.nanoTime());
	}
	
	/**
	 * Create {@link Column} with given key/value and  timestamp.
	 */
	public static Column createColumn(ByteBuffer name, ByteBuffer value, Long timestamp) {
		Column c = new Column();
		c.setName(name);
		c.setValue(value);
		c.setTimestamp(timestamp);
		return c;
	}
	
	/**
	 * Create {@link SuperColumn} with supplied name (converted to UTF-8 bytes) and columns 
	 */
	public static SuperColumn createSuperColumn(String key, List<Column> columns) {
		return createSuperColumn(bytesFromString(key), columns);
		
	}
	
	/**
	 * Create {@link SuperColumn} with supplied name and columns 
	 */
	public static SuperColumn createSuperColumn(ByteBuffer key, List<Column> columns) {
		SuperColumn c = new SuperColumn();
		c.setName(key);
		c.setColumns(columns);
		return c;
	} 
	
	/**
	 * Create {@link ColumnOrSuperColumn} from given {@link Column}
	 */
	public static ColumnOrSuperColumn createOrFromColumn(Column col) {
		ColumnOrSuperColumn c = new ColumnOrSuperColumn();
		c.setColumn(col);
		return c;
	}

	/**
	 * Create {@link ColumnOrSuperColumn} from given {@link SuperColumn}
	 */
	public static ColumnOrSuperColumn createOrFromColumn(SuperColumn col) {
		ColumnOrSuperColumn c = new ColumnOrSuperColumn();
		c.setSuper_column(col);
		return c;
	}

	/**
	 * Create Mutation object from given {@link ColumnOrSuperColumn}
	 * @param cos
	 * @return
	 */
	public static Mutation createMutation(ColumnOrSuperColumn cos) {
		Mutation m = new Mutation();
		m.setColumn_or_supercolumn(cos);
		return m;
	}
	
	/**
	 * Get default application character set. For now UTF-8 
	 */
	public static Charset getCharset() {
		return defaultCharset;
	}
	
	/**
	 * Convert java string to ByteBuffer with default application charset
	 */
	public static ByteBuffer bytesFromString(String s) {
		return ByteBuffer.wrap(s.getBytes(defaultCharset));
	}

	/**
	 * Convert bytes stored in db to java string using default application charset
	 */
	public static String stringFromBytes(ByteBuffer b) {
		return defaultCharset.decode(b).toString();
	}
	
}
